package com.ruoyi.socket.api;

import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.gams.domain.GamsDevice;
import com.ruoyi.gams.domain.GamsUserQueue;
import com.ruoyi.gams.mapper.GamsDeviceBussinessRelationMapper;
import com.ruoyi.gams.mapper.GamsDeviceMapper;
import com.ruoyi.gams.mapper.GamsUserQueueMapper;
import com.ruoyi.gams.vo.Result;
import com.ruoyi.gams.vo.param.QueueLoginParam;
import com.ruoyi.socket.screen.ScreenVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "4.综合屏接口")
@RestController
@RequestMapping("/queue-api")
public class QueueController
{

    @Autowired
    private GamsUserQueueMapper userQueueMapper;

    @Autowired
    private GamsDeviceMapper deviceMapper;

    @Autowired
    private GamsDeviceBussinessRelationMapper relationMapper;

    @ApiOperation("2.0 登录")
    @ResponseBody
    @PostMapping("/login")
    public Result<GamsDevice> login(@RequestBody QueueLoginParam param)
    {
        if (StringUtils.isEmpty(param.getPassword()))
        {
            throw new GlobalException("密码不能为空");
        }
        String screenId = param.getScreenId();
        if (StringUtils.isEmpty(screenId))
        {
            throw new GlobalException("设备编号不能为空");
        }

        GamsDevice device = deviceMapper.findFirstByDeviceId(screenId);
        if (device == null)
        {
            throw new GlobalException("设备编号错误");
        }

        if (!param.getPassword().equals(device.getPassword()))
        {
            throw new GlobalException("密码错误");
        }

        return Result.success(device);
    }

    @ApiOperation("2.0 获取未完成的排队信息")
    @ResponseBody
    @GetMapping("/getList")
    public Result<List<ScreenVO>> getList(Integer deviceId)
    {
        List<Integer> ids = relationMapper.findBussinessIdByDeviceId(deviceId, 3);
        if (CollectionUtils.isEmpty(ids))
        {
            return Result.success();
        }
        String[] idsStr = ids.stream().map(Object::toString).toArray(String[]::new);
        GamsUserQueue gamsUserQueue = new GamsUserQueue();
        gamsUserQueue.setBussinessIds(idsStr);
        gamsUserQueue.setNotdone(1);
        gamsUserQueue.setIsToday(1);
        List<GamsUserQueue> list = userQueueMapper.selectGamsUserQueueList(gamsUserQueue);
        List<ScreenVO> screenVOS = ScreenVO.userQueueList2ScreenVOList2(list);
        return Result.success(screenVOS);
    }

}
